#include "DataBaseManager.h"
#include "Common.h"
#include "ReaderManager.h"
#include "TreeItem_Chapter.h"
#include "Widget_BookList.h"

#include <QtCore/QDir>
#include <QtCore/QObject>
#include <QtGui/QGridLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QSplitter>

namespace Jonathan
{

   /**
    * @brief Initializes a new book list widget.
    *
    * @param parent Parent widget hosting book list.
    */
   Widget_BookList::Widget_BookList(QWidget *parent) :
       QWidget(parent)
   {
      QSplitter* splitter = new QSplitter (Qt::Vertical, this);
      splitter->setChildrenCollapsible (false);

      this->m_BookTree = new QTreeWidget (splitter);
      this->m_BookTree->setHeaderHidden (true);
      connect (this->m_BookTree, SIGNAL (itemExpanded (QTreeWidgetItem*)),
               this, SLOT (LoadItemData (QTreeWidgetItem*)));
      connect (this->m_BookTree, SIGNAL (itemDoubleClicked (QTreeWidgetItem*,int)),
               this, SLOT (TreeItemDoubleClicked (QTreeWidgetItem *)));

      splitter->addWidget (this->CreateTreePanel (this->m_BookTree, splitter, "Books"));

      this->m_ReferenceTree = new QTreeWidget (splitter);
      this->m_ReferenceTree->setHeaderHidden (true);
      splitter->addWidget (this->CreateTreePanel (this->m_ReferenceTree, splitter, "Reference"));

      QGridLayout* layout = new QGridLayout (this);
      layout->setMargin (0);
      layout->setSpacing (0);
      layout->addWidget (splitter);
      this->setLayout (layout);

      LoadBooks ();
   }

   /**
    * @brief Initializes a new book list widget.
    *
    * @param tree Tree to add to panel.
    * @param parent Parent widget hosting book list.
    * @param text Text for panel's title.
    * @return QWidget* Newly created panel.
    */
   QWidget* Widget_BookList::CreateTreePanel (QTreeWidget* tree, QWidget* parent, QString text)
   {
      QWidget* titlePanel = new QWidget (parent);
      titlePanel->setStyleSheet ("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(214, 214, 214, 255), stop:1 rgba(240, 240, 240, 255));");
      QLabel* label = new QLabel (titlePanel);
      label->setText (text);
      label->setFont (QFont ("Tahoma", 9, 75));
      QLineEdit* searchBox = new QLineEdit ();
      searchBox->setPlaceholderText ("Search...");
      searchBox->setStyleSheet ("background-color: rgb(255, 255, 255);");

      QGridLayout* layout = new QGridLayout (titlePanel);
      layout->setMargin (3);
      layout->setSpacing (0);
      layout->setHorizontalSpacing (15);
      layout->addWidget (label, 0, 0);
      layout->addWidget (searchBox, 0, 1);
      titlePanel->setLayout (layout);

      QWidget* panel = new QWidget (parent);
      layout = new QGridLayout (panel);
      layout->setMargin (0);
      layout->setSpacing (0);
      layout->addWidget (titlePanel, 0, 0);
      layout->addWidget (tree, 1, 0);
      panel->setLayout (layout);

      return panel;
   }

   /**
    * @brief Loads the list of available books.
    *
    */
   void Widget_BookList::LoadBooks ()
   {
      QString dataDirectory = QDir::currentPath () + QDir::separator () + "Data";
      QDir dir (dataDirectory);
      QStringList files = dir.entryList (QStringList ("*.db"), QDir::Files | QDir::NoSymLinks);
      foreach (QString file, files)
      {
         TreeItem_Book* book = DataBaseManager::GetInstance ()->AddBook (dataDirectory + QDir::separator () + file,
                                                                         this->m_BookTree, this->m_ReferenceTree);
         if (book != NULL)
         {
            if (book->GetBookType () == "Reference")
               this->m_ReferenceTree->addTopLevelItem (book);
            else
               this->m_BookTree->addTopLevelItem (book);
         }
      }
   }

   /**
    * @brief Loads any children for the item being expaned.
    *
    * @param treeItem
    */
   void Widget_BookList::LoadItemData (QTreeWidgetItem *treeItem)
   {
      if (treeItem->childCount () == 1)
      {
         IBookListItem *bookItem = reinterpret_cast<IBookListItem*> (treeItem);
         QTreeWidgetItem *childItem = treeItem->child (0);

         if (bookItem->GetBookItemType () == Jonathan::Book)
         {
            treeItem->removeChild (childItem);
            TreeItem_Chapter *prefaceItem = new TreeItem_Chapter (treeItem);
            treeItem->addChild (prefaceItem);
         }
         else if (childItem->text (0).isNull () || childItem->text (0).isEmpty ())
            treeItem->removeChild (childItem);
         else
            return;

         bookItem->LoadChildren ();
      }
   }

   void Widget_BookList::TreeItemDoubleClicked (QTreeWidgetItem *treeItem)
   {
      IBookListItem *bookTreeItem = reinterpret_cast<IBookListItem*> (treeItem);
      if (bookTreeItem->GetBookItemType () == Jonathan::Preface ||
          bookTreeItem->GetBookItemType () == Jonathan::Chapter)
      {
         QString subBook;
         TreeItem_Book *bookItem;
         IBookListItem *parent = reinterpret_cast<IBookListItem*> (treeItem->parent ());
         if (parent->GetBookItemType () == Jonathan::SubBook)
         {
            subBook = parent->GetName ();
            bookItem = reinterpret_cast<TreeItem_Book*> (parent->parent ());
         }
         else
            bookItem = reinterpret_cast<TreeItem_Book*> (parent);

         ReaderManager::GetInstance ()->StartReading (bookItem, subBook, bookTreeItem->GetName ());
      }
   }

}
